package com.pj.point.pri;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.pj.point.R;
import com.pj.point.adapter.HomeListAdapter;
import com.pj.point.litepal.DiaryJavaBean;

import org.litepal.LitePal;

import java.util.List;
import java.util.stream.Collectors;

public class Private extends AppCompatActivity {

    private ListView privateListView;


    private List<DiaryJavaBean> all;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_private);
        initBar();
        initView();
        getData();
    }

    private void getData() {
        all = LitePal.findAll(DiaryJavaBean.class,true);
        if (all != null) {
            all = all.stream().filter(DiaryJavaBean::getIsPrivate).collect(Collectors.toList());
            if (all.size() != 0) {
                privateListView.setAdapter(new HomeListAdapter(all));
                privateListView.setOnItemClickListener((parent, view, position, id) -> {
                    Intent intent = new Intent(Private.this, PrivateDiary.class);
                    intent.putExtra("isOpen", all.get(position).getCreateTime());
                    startActivityForResult(intent,1);
                });
            }else{
                Toast.makeText(this, "暂无抽屉日记", Toast.LENGTH_SHORT).show();
                privateListView.setAdapter(new HomeListAdapter(all));
            }
        }
    }

    private void initView() {
        privateListView = (ListView) findViewById(R.id.private_grid_view);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.private_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.private_menu_change_pwd) {

        } else {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initBar() {
        ActionBar bar = getSupportActionBar();
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setTitle("抽屉");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1){
            getData();
        }
    }
}